/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2010 huliqing, huliqing.cn@gmail.com
 *
 * This file is part of QFaces.
 * QFaces is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QFaces is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with QFaces.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 这个文件是QFaces的一部分。
 * 您可以单独使用或分发这个文件，但请不要移除这个头部声明信息.
 * QFaces是一个自由软件，您可以自由分发、修改其中的源代码或者重新发布它，
 * 新的任何修改后的重新发布版必须同样在遵守LGPL3或更后续的版本协议下发布.
 * 关于LGPL协议的细则请参考COPYING、COPYING.LESSER文件，
 * 您可以在QFaces的相关目录中获得LGPL协议的副本，
 * 如果没有找到，请连接到 http://www.gnu.org/licenses/ 查看并获取副本。
 *
 * - Author: Huliqing
 * - Contact: huliqing.cn@gmail.com
 * - License: GNU Lesser General Public License (LGPL)
 * - Blog and source code availability: http://www.huliqing.name/
 */

package name.huliqing.qfaces.component;

import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import name.huliqing.qfaces.QFaces;

/**
 * @author huliqing
 *
 */
public class Helper extends UIComponentBase {

    // 所绑定的用于调用后台帮助信息的method
    private MethodExpression listener;
    // true：ajax方式调用，否则直接渲染在页面
    private Boolean lazy;
    // 帮助信息，如果绑定了message,那么将不通过listener从后台获取数据,
    private String message;
    // 图片按钮的路径(src),客户端组件传递过来的值，如果为null,则使用默认的imageDefault
    private String image;
    // 按钮的tip提示信息
    private String tip;
    // 内容的style（不含Frame）
    private String style;
    // 内容的class样式
    private String styleClass;
    private Frame frame;
    private String loadType;
    private String helpId;
    // content区域的宽度
    private String width;

    public Helper() {
        super();
        setRendererType("name.huliqing.qfaces.render.HelperRenderer");
    }

    @Override
    public String getFamily() {
        return "name.huliqing.qfaces.render.HelperFamily";
    }
    private Object[] _values;

    @Override
    public void restoreState(FacesContext fc, Object state) {
        _values = (Object[]) state;
        super.restoreState(fc, _values[0]);
        this.listener = (MethodExpression) _values[1];
        this.lazy = (Boolean) _values[2];
        this.message = (String) _values[3];
        this.image = (String) _values[4];
        this.tip = (String) _values[5];
        this.style = (String) _values[6];
        this.styleClass = (String) _values[7];
        this.frame = (Frame) _values[8];
        this.loadType = (String) _values[9];
        this.helpId = (String) _values[10];
        this.width = (String) _values[11];
    }

    @Override
    public Object saveState(FacesContext fc) {
        if (_values == null) {
            _values = new Object[12];
        }
        _values[0] = super.saveState(fc);
        _values[1] = this.listener;
        _values[2] = this.lazy;
        _values[3] = this.message;
        _values[4] = this.image;
        _values[5] = this.tip;
        _values[6] = this.style;
        _values[7] = this.styleClass;
        _values[8] = this.frame;
        _values[9] = this.loadType;
        _values[10] = this.helpId;
        _values[11] = this.width;
        return _values;
    }

    private Object getFromExp(String name) {
        ValueExpression _ve = getValueExpression(name);
        if (_ve != null) {
            return _ve.getValue(getFacesContext().getELContext());
        } else {
            return null;
        }
    }

    // ---- Getter and Setter
    public MethodExpression getListener() {
        return this.listener;
    }

    public void setListener(MethodExpression listener) {
        this.listener = listener;
    }

    public void setListenerAsExpression(String methodExpression) {
        MethodExpression me = QFaces.createMethodExpression(methodExpression, String.class, new Class[]{});
        setListener(me);
    }

    public Boolean getLazy() {
        if (lazy != null) {
            return lazy;
        }
        return (Boolean) this.getFromExp("lazy");
    }

    public void setLazy(Boolean lazy) {
        this.lazy = lazy;
    }

    public String getMessage() {
        if (message != null) {
            return message;
        }
        return (String) this.getFromExp("message");
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getImage() {
        if (image != null) {
            return image;
        }
        return (String) this.getFromExp("image");
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getTip() {
        if (tip != null) {
            return tip;
        }
        return (String) this.getFromExp("tip");
    }

    public void setTip(String tip) {
        this.tip = tip;
    }

    public String getStyle() {
        if (style != null) {
            return style;
        }
        return (String) this.getFromExp("style");
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public String getStyleClass() {
        if (styleClass != null) {
            return styleClass;
        }
        return (String) this.getFromExp("styleClass");
    }

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    public Frame getFrame() {
        // frame内部组件不能从ValueExpression中绑定
        return frame;
    }

    public void setFrame(Frame frame) {
        this.frame = frame;
    }

    public String getLoadType() {
        if (loadType != null) {
            return loadType;
        }
        return (String) this.getFromExp("loadType");
    }

    public void setLoadType(String loadType) {
        this.loadType = loadType;
    }

    public String getHelpId() {
        if (helpId != null) {
            return helpId;
        }
        return (String) this.getFromExp("helpId");
    }

    public void setHelpId(String helpId) {
        this.helpId = helpId;
    }

    public String getWidth() {
        if (width != null) {
            return width;
        }
        return (String) this.getFromExp("width");
    }

    public void setWidth(String width) {
        this.width = width;
    }
}
